/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Reflection;
using System.Collections;
using System.IO;
using _videoConference.BAL;
using System.ServiceModel;


namespace _videoConference
{ 
    public partial class GenerateModule : System.Windows.Controls.UserControl,IDisposable
    {
        int count = 0;
        DataSet ds1;
        public Assembly a;
        Assembly b = Assembly.GetAssembly(typeof(GenerateModule));
        string[] podproperty;

        ArrayList al = new ArrayList();
        IAddPage ia = new IAddPage();        

        double pLeft;
        double pTop;
        double pWidth;
        double pHeight;

        double oldHeight;
        double oldWidth;
        double oldLeft;
        double oldTop;
        
        bool mMove = false;

        private bool disposed = false;

        public GenerateModule()
        {
            InitializeComponent();

            if (UserInfo.strMeetingMode == "Meeting Mode")
            {
                ((PodResizeClass)PeerServices.objectPodResize).ENewJoin += new PodResizeClass.UserJoin(GenerateModule_ENewJoin);
                ((PodResizeClass)PeerServices.objectPodResize).EResetPod += new PodResizeClass.ResetPod(GenerateModule_EResetPod);
                ((PodResizeClass)PeerServices.objectPodResize).EMinPod += new PodResizeClass.MinPod(GenerateModule_EMinPod);
                ((PodResizeClass)PeerServices.objectPodResize).EMaxPod += new PodResizeClass.MaxPod(GenerateModule_EMaxPod);
                ((PodResizeClass)PeerServices.objectPodResize).ESetPod += new PodResizeClass.SetPodLayout(GenerateModule_ESetPod);
            }
            cnvMain.Tag = "";           
            ds1 = ia.getmodulename();
            cmbModuleName.Items.Clear();
            if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    ComboBoxItem cmbIT = new ComboBoxItem();
                    cmbIT.Content = ds1.Tables[0].Rows[i][0].ToString();
                    cmbIT.Background = Brushes.Black;
                    cmbIT.Foreground = Brushes.White;
                    cmbModuleName.Items.Add(cmbIT);
                }
            }
            cmbModuleName.SelectionChanged += new SelectionChangedEventHandler(cmbModuleName_SelectionChanged);
            btnUpdate.Click += new RoutedEventHandler(btnUpdate_Click);           
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);            
            cnvMain.SizeChanged += new SizeChangedEventHandler(cnvMain_SizeChanged);
            
            string controlfolder = b.Location.Replace("1videoConference.exe", @"Presentation Layer");            
            DirectoryInfo dirinfocontrol = new DirectoryInfo(controlfolder);
            ShowDirectory(dirinfocontrol);
            
            string balfolder = b.Location.Replace("1videoConference.exe", @"Business Layer");            
            DirectoryInfo dirinfobal = new DirectoryInfo(balfolder);
            ShowDirectory(dirinfobal);
            
            string dalfolder = b.Location.Replace("1videoConference.exe", @"Data Layer");            
            DirectoryInfo dirinfodal = new DirectoryInfo(dalfolder);
            ShowDirectory(dirinfodal);
            //cnvMain.PreviewKeyDown += new KeyEventHandler(cnvMain_PreviewKeyDown);
            //cnvMain.KeyDown += new KeyEventHandler(cnvMain_KeyDown);
            
        }

        void GenerateModule_ESetPod(string[] podproperty1, string uname,string meetingid)
        {
            if (UserInfo.UserName == uname && UserInfo.MeetingID==meetingid)
            {
                if (UserInfo.MyRole != "Host")
                {
                    try
                    {
                        int m = 0;
                        for(int j=0;j<podproperty1.Length;j+=8)
                        {
                            for (int i = 0; i < cnvMain.Children.Count; i++)
                            {
                                if (((ctlMyPOD)cnvMain.Children[i]).PID == int.Parse(podproperty1[j]))
                                {
                                    ((ctlMyPOD)cnvMain.Children[i]).Height = double.Parse(podproperty1[j + 1]);
                                    ((ctlMyPOD)cnvMain.Children[i]).Width = double.Parse(podproperty1[j + 2]);
                                    ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.LeftProperty, double.Parse(podproperty1[j + 3]));
                                    ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.TopProperty, double.Parse(podproperty1[j + 4]));
                                    ((ctlMyPOD)cnvMain.Children[i]).Text = podproperty1[j + 5];
                                    ((ctlMyPOD)cnvMain.Children[i]).ControlName = podproperty1[j + 6];
                                    //MessageBox.Show(podproperty1[j + 6]);
                                    if (podproperty1[j + 7].ToLower() == "visible")
                                    {
                                        try
                                        {
                                            ((ctlMyPOD)cnvMain.Children[i]).PodVisible(true);
                                        }
                                        catch (Exception exp)
                                        {
                                            exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--GenerateModule_ESetPod()--:----:--");
                                            clsUtility.LogException = exp;
                                            //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:----:--" + exp.Message);
                                        }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            ((ctlMyPOD)cnvMain.Children[i]).PodVisible(false);
                                        }
                                        catch (Exception exp)
                                        {
                                            exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--GenerateModule_ESetPod1()--:----:--");
                                            clsUtility.LogException = exp;
                                            //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:--GenerateModule_ESetPod()--:--" + exp.Message);
                                        }
                                    }                                    
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--GenerateModule_ESetPod2()--:----:--");
                        clsUtility.LogException = exp;
                        //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:--GenerateModule_ESetPod2()--:--" + exp.Message);
                    }
                }
            }
        }        

        void GenerateModule_EMaxPod(double setLeft, double setTop, double setWidth, double setHeight, int PID, bool MAXID,string meetingid)
        {
            if (UserInfo.MyRole != "Host" && UserInfo.MeetingID==meetingid)
            {
                for (int i = 0; i < cnvMain.Children.Count; i++)
                {
                    if (((ctlMyPOD)cnvMain.Children[i]).PID == PID)
                    {
                        if (MAXID == true)
                        {
                            ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.LeftProperty, 5.0);
                            ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.TopProperty, 5.0);
                            ((ctlMyPOD)cnvMain.Children[i]).Height = 450;
                            ((ctlMyPOD)cnvMain.Children[i]).Width = 600;
                            ((ctlMyPOD)cnvMain.Children[i]).maxid = false;
                            ((ctlMyPOD)cnvMain.Children[i]).ctlMyPOD_PreviewMouseDown(cnvMain.Children[i], new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left));
                            return;
                        }
                        else
                        {
                            ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.LeftProperty, setLeft);
                            ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.TopProperty, setTop);
                            ((ctlMyPOD)cnvMain.Children[i]).Height = setHeight;
                            ((ctlMyPOD)cnvMain.Children[i]).Width = setWidth;
                            ((ctlMyPOD)cnvMain.Children[i]).maxid = true;
                            ((ctlMyPOD)cnvMain.Children[i]).ctlMyPOD_PreviewMouseDown(cnvMain.Children[i], new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left));
                            return;
                        }
                    }
                }
            }
        }

        void GenerateModule_EMinPod(int PID,string meetingid)
        {
            if (UserInfo.MeetingID == meetingid)
            {
                for (int i = 0; i < cnvMain.Children.Count; i++)
                {
                    if (((ctlMyPOD)cnvMain.Children[i]).PID == PID)
                    {
                        ((ctlMyPOD)cnvMain.Children[i]).PodVisible(false);
                    }
                }
            }
        }

        void GenerateModule_EResetPod(double setLeft, double setTop, double setWidth, double setHeight, int PID,string meetingid)
        {
            if (UserInfo.MyRole != "Host" && UserInfo.MeetingID==meetingid)
            {
                for (int i = 0; i < cnvMain.Children.Count; i++)
                {
                    if (((ctlMyPOD)cnvMain.Children[i]).PID == PID)
                    {
                        ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.LeftProperty, setLeft);
                        ((ctlMyPOD)cnvMain.Children[i]).SetValue(InkCanvas.TopProperty, setTop);
                        ((ctlMyPOD)cnvMain.Children[i]).Height = setHeight;
                        ((ctlMyPOD)cnvMain.Children[i]).Width = setWidth;
                        ((ctlMyPOD)cnvMain.Children[i]).ctlMyPOD_PreviewMouseDown(cnvMain.Children[i], new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left));
                        break;
                    }
                }
            }
        }

        void GenerateModule_ENewJoin(string uname,string meetingid)
        {
            int m = 0;
            if (UserInfo.MyRole == "Host" && UserInfo.UserName!=uname && UserInfo.MeetingID==meetingid)
            {
                int arraysize = 8 * cnvMain.Children.Count;

                podproperty = new string[arraysize];

                for (int i = 0; i < cnvMain.Children.Count; i++)
                {
                    while (m < arraysize)
                    {
                        podproperty[m] = ((ctlMyPOD)cnvMain.Children[i]).PID.ToString();
                        podproperty[m + 1] = ((ctlMyPOD)cnvMain.Children[i]).Height.ToString();
                        podproperty[m + 2] = ((ctlMyPOD)cnvMain.Children[i]).Width.ToString();
                        podproperty[m + 3] = ((ctlMyPOD)cnvMain.Children[i]).GetValue(InkCanvas.LeftProperty).ToString();
                        podproperty[m + 4] = ((ctlMyPOD)cnvMain.Children[i]).GetValue(InkCanvas.TopProperty).ToString();
                        podproperty[m + 5] = ((ctlMyPOD)cnvMain.Children[i]).Text;
                        podproperty[m + 6] = ((ctlMyPOD)cnvMain.Children[i]).ControlName;
                        podproperty[m + 7] = ((ctlMyPOD)cnvMain.Children[i]).Visibility.ToString();
                        m += 8;
                        break;
                    }
                }
                //MessageBox.Show(pagename);

                PeerServices.channelPageNav.svcSetPageName(pgname,UserInfo.MeetingID);
                PeerServices.channelPodResize.svcSetPodLayout(podproperty, uname,UserInfo.MeetingID);              
            }
        }    

        void cnvMain_SizeChanged(object sender, SizeChangedEventArgs e)
        {            
            if (double.Parse(e.NewSize.Height.ToString()) >= 355.0)
            {
                GridLength gl = new GridLength(e.NewSize.Height+35.0, GridUnitType.Pixel);
                ((RowDefinition)((Grid)(((InkCanvas)((InkCanvas)this.Parent).Parent).Parent)).FindName("rd1")).Height = gl;
                canvasmain.Height = e.NewSize.Height+35.0+10.0;
            }
            else
            {
                GridLength gl = new GridLength(420.0, GridUnitType.Pixel);
                ((RowDefinition)((Grid)(((InkCanvas)((InkCanvas)this.Parent).Parent).Parent)).FindName("rd1")).Height = gl;
                canvasmain.Height = 420.0;
            }
            if (double.Parse(e.NewSize.Width.ToString()) >= 1024.0)
            {
                GridLength gl = new GridLength(e.NewSize.Width, GridUnitType.Pixel);
                ((ColumnDefinition)((Grid)(((InkCanvas)((InkCanvas)this.Parent).Parent).Parent)).FindName("cd1")).Width = gl;
                canvasmain.Width = e.NewSize.Width;
            }
            else
            {
                GridLength gl = new GridLength(1024.0, GridUnitType.Pixel);
                ((ColumnDefinition)((Grid)(((InkCanvas)((InkCanvas)this.Parent).Parent).Parent)).FindName("cd1")).Width = gl;
                canvasmain.Width = 1024.0;
            }

        }

        void cnvMain_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {          
            for (int i = 0; i < cnvMain.Children.Count; i++)
            {
                if (((System.Windows.Controls.Button)((ctlMyPOD)cnvMain.Children[i]).FindName("btnMaximized")).IsMouseOver)
                {
                    if (((ctlMyPOD)cnvMain.Children[i]).Width < 600.0 && ((ctlMyPOD)cnvMain.Children[i]).Height < 450.0)
                    {
                        oldLeft = double.Parse(((ctlMyPOD)cnvMain.Children[i]).GetValue(InkCanvas.LeftProperty).ToString());
                        oldTop = double.Parse(((ctlMyPOD)cnvMain.Children[i]).GetValue(InkCanvas.TopProperty).ToString());
                        oldWidth = ((ctlMyPOD)cnvMain.Children[i]).Width;
                        oldHeight = ((ctlMyPOD)cnvMain.Children[i]).Height;
                    }
                    break;
                }
            }
        }

        string pgname = "";

        public string pagename
        {
            get
            {
                return pgname;
            }
            set
            {
                pgname = value;
                lblpgname.Content = pgname;
                displayLayout(pgname);
            }
        }

        public void newlayout()
        {           
            if (UserInfo.strMeetingMode == "Meeting Mode")
            {
                int ctl = 0;

                DataSet ds = ia.layoutid();
                inkcontainer.Visibility = Visibility.Collapsed;
                inkcontainer.Height = 0.0;
                cnvMain.SetValue(Canvas.TopProperty, 0.0);

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    for (int Layoutcount = 0; Layoutcount < ds.Tables[0].Rows.Count; Layoutcount++)
                    {
                        DataSet ds1 = ia.setpod(int.Parse(ds.Tables[0].Rows[Layoutcount][0].ToString()));

                        for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                        {
                            ctlMyPOD c = new ctlMyPOD();
                            c.PID = int.Parse(ds1.Tables[0].Rows[i]["PodID"].ToString());
                            c.Height = double.Parse(ds1.Tables[0].Rows[i]["PodHeight"].ToString());
                            c.Width = double.Parse(ds1.Tables[0].Rows[i]["PodWidth"].ToString());
                            c.SetValue(InkCanvas.LeftProperty, double.Parse(ds1.Tables[0].Rows[i]["PodLeft"].ToString()));
                            c.SetValue(InkCanvas.TopProperty, double.Parse(ds1.Tables[0].Rows[i]["PODTop"].ToString()));
                            c.Text = ds1.Tables[0].Rows[i]["PodTitle"].ToString();
                            c.ControlName = ds1.Tables[0].Rows[i]["ControlName"].ToString();
                            
                            DataSet ds2 = ia.getcontrolname(c.ControlName);
                            for (int j = 0; j < al.Count; j++)
                            {
                                string[] arraysplit = al[j].ToString().Split('\\');
                                if (arraysplit[arraysplit.Length - 1].ToString() == ds2.Tables[0].Rows[0][0].ToString())
                                {
                                    a = Assembly.LoadFrom(al[j].ToString());
                                    AssemblyName[] an = a.GetReferencedAssemblies();

                                    for (int alcount = 0; alcount < al.Count; alcount++)
                                    {
                                        string strsplit = al[alcount].ToString();
                                        string[] strold = strsplit.Split('\\');
                                        string strnew = strold[strold.Length - 1].Substring(0, strold[strold.Length - 1].Length - 4);

                                        for (int asscount = 0; asscount < an.Length; asscount++)
                                        {
                                            if (an[asscount].Name == strnew)
                                            {
                                                Assembly assbal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[alcount].ToString()).GetName());
                                                AssemblyName[] anbal = assbal.GetReferencedAssemblies();
                                                for (int andal = 0; andal < al.Count; andal++)
                                                {
                                                    string strsplitdal = al[andal].ToString();
                                                    string[] strolddal = strsplitdal.Split('\\');
                                                    string strnewdal = strolddal[strolddal.Length - 1].Substring(0, strolddal[strolddal.Length - 1].Length - 4);

                                                    for (int asscountdal = 0; asscountdal < anbal.Length; asscountdal++)
                                                    {
                                                        if (anbal[asscountdal].Name == strnewdal)
                                                        {
                                                            Assembly assdal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[andal].ToString()).GetName());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    Assembly[] assb = System.AppDomain.CurrentDomain.GetAssemblies();
                                    Type[] t1 = a.GetTypes();

                                    for (int k = 0; k < t1.Length; k++)
                                    {
                                        if (t1[k].Name== ds2.Tables[0].Rows[0][1].ToString())
                                        {
                                            try
                                            {
                                                 object obj1 = Activator.CreateInstance(t1[k]);
                                                ((Canvas)c.FindName("cnvBody")).Tag = t1[k].ToString();
                                                ((UserControl)obj1).SetValue(Canvas.LeftProperty, 0.0);
                                                ((UserControl)obj1).SetValue(Canvas.TopProperty, 0.0);
                                                if (((Canvas)c.FindName("cnvBody")).Height - 3.0 >= 0)
                                                {
                                                    ((UserControl)obj1).Height = ((Canvas)c.FindName("cnvBody")).Height - 3.0;
                                                }
                                                else
                                                {
                                                    ((UserControl)obj1).Height = 0.0;
                                                }
                                                if (((Canvas)c.FindName("cnvBody")).Width - 3.0 >= 0)
                                                {
                                                    ((UserControl)obj1).Height = ((Canvas)c.FindName("cnvBody")).Width - 3.0;
                                                }
                                                else
                                                {
                                                    ((UserControl)obj1).Width = 0.0;
                                                }
                                                
                                                ((Canvas)c.FindName("cnvBody")).Children.Add((UIElement)obj1);
                                                c.PodVisible(false);
                                                cnvMain.Children.Add(c);
                                            }
                                            catch (Exception exp)
                                            {
                                                //MessageBox.Show("New layout" + exp.Message);
                                                exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--newlayout()1--:--");
                                                clsUtility.LogException = exp;
                                                //MessageBox.Show("Object Creation"+exp.GetBaseException().Message + "for" + t1[k].ToString());
                                            }
                                        }
                                    }
                                }
                            }                          
                        }
                    }
                }
            }

            if (UserInfo.strMeetingMode == "Meeting Mode")
            {
                try
                {
                    PeerServices.channelPodResize.svcJoin(UserInfo.UserName,UserInfo.MeetingID);
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--newlayout()2--:--");
                    clsUtility.LogException = exp;
                    //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:--newlayout()--:--" + exp.Message);
                }
            }
        }

        public void displayLayout(string layoutname)
        {           
            int ctl=0;          
           
            try
            {
                for (int i = 0; i < cnvMain.Children.Count; i++)
                {
                    ((ctlMyPOD)(cnvMain.Children[i])).PodVisible(false);
                }
                if (UserInfo.strMeetingMode == "Design Mode")
                {
                    DataSet dsmod = ia.getmodulename();
                    cmbModuleName.Items.Clear();
                    for (int i = 0; i < dsmod.Tables[0].Rows.Count; i++)
                    {
                        ComboBoxItem cmbIT = new ComboBoxItem();
                        cmbIT.Content = dsmod.Tables[0].Rows[i][0].ToString();
                        cmbIT.Background = Brushes.Black;
                        cmbIT.Foreground = Brushes.White;
                        cmbModuleName.Items.Add(cmbIT);
                    }

                    cnvMain.Children.Clear();
                    DataSet ds = ia.setlayout(layoutname);
                    inkcontainer.Visibility = Visibility.Visible;
                    inkcontainer.Height = 35.0;
                    cnvMain.SetValue(Canvas.TopProperty, 35.0);

                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        DataSet ds1 = ia.setpod(int.Parse(ds.Tables[0].Rows[0][0].ToString()));
                        for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                        {
                            ctlMyPOD c = new ctlMyPOD();
                            c.Height = double.Parse(ds1.Tables[0].Rows[i]["PodHeight"].ToString());
                            c.Width = double.Parse(ds1.Tables[0].Rows[i]["PodWidth"].ToString());
                            c.SetValue(InkCanvas.LeftProperty, double.Parse(ds1.Tables[0].Rows[i]["PodLeft"].ToString()));
                            c.SetValue(InkCanvas.TopProperty, double.Parse(ds1.Tables[0].Rows[i]["PODTop"].ToString()));
                            c.Text = ds1.Tables[0].Rows[i]["PodTitle"].ToString();
                            c.ControlName = ds1.Tables[0].Rows[i]["ControlName"].ToString();
                            Label l = new Label();
                            l.Background = ((Rectangle)c.FindName("rctTitle")).Fill;
                            l.Foreground = ((Rectangle)c.FindName("rctTitle")).Stroke;
                            l.Content = ds1.Tables[0].Rows[i]["ControlName"].ToString();
                            l.Height = ((Label)c.FindName("lblTitle")).Height;
                            l.Width = ((Rectangle)c.FindName("rctTitle")).Width;
                            ((Canvas)c.FindName("cnvBody")).Children.Add(l);
                            ((Canvas)c.FindName("cnvBody")).Tag = ds1.Tables[0].Rows[i]["ControlName"].ToString();
                            c.ModID =int.Parse(ds1.Tables[0].Rows[i]["ModID"].ToString());
                            //((Canvas)c.FindName("cnvBody")).Tag = ds1.Tables[0].Rows[i]["ModID"].ToString();
                            cnvMain.Children.Add(c);
                            c = null;
                        }
                    }
                }
                else if (UserInfo.strMeetingMode == "Meeting Mode")
                {
                    DataSet ds = ia.setlayout(layoutname);
                    inkcontainer.Visibility = Visibility.Collapsed;
                    inkcontainer.Height = 0.0;
                    cnvMain.SetValue(Canvas.TopProperty, 0.0);
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        DataSet ds1 = ia.setpod(int.Parse(ds.Tables[0].Rows[0][0].ToString()));

                        for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                        {
                            for (ctl = 0; ctl < cnvMain.Children.Count; ctl++)
                            {                                
                                if (((ctlMyPOD)cnvMain.Children[ctl]).ControlName == ds1.Tables[0].Rows[i]["ControlName"].ToString())
                                {                                    
                                    ((ctlMyPOD)cnvMain.Children[ctl]).PodVisible(true);
                                    ((ctlMyPOD)cnvMain.Children[ctl]).PID = int.Parse(ds1.Tables[0].Rows[i]["PodID"].ToString());
                                    ((ctlMyPOD)cnvMain.Children[ctl]).Height = double.Parse(ds1.Tables[0].Rows[i]["PodHeight"].ToString());
                                    ((ctlMyPOD)cnvMain.Children[ctl]).Width = double.Parse(ds1.Tables[0].Rows[i]["PodWidth"].ToString());
                                    ((ctlMyPOD)cnvMain.Children[ctl]).SetValue(InkCanvas.LeftProperty, double.Parse(ds1.Tables[0].Rows[i]["PodLeft"].ToString()));
                                    ((ctlMyPOD)cnvMain.Children[ctl]).SetValue(InkCanvas.TopProperty, double.Parse(ds1.Tables[0].Rows[i]["PODTop"].ToString()));
                                    ((ctlMyPOD)cnvMain.Children[ctl]).Text = ds1.Tables[0].Rows[i]["PodTitle"].ToString();

                                    break;
                                }                              
                            }                            
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("Display Layout" + exp.Message);
                exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--displaylayout()1--:--");
                clsUtility.LogException = exp;
                //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:--displayLayout()--:--" + exp.Message);
            }
        }
     
        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ((Menu)((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvmenu")).FindName("mnu")).IsEnabled = true;
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        void btnUpdate_Click(object sender, RoutedEventArgs e)
        {            
            try
            {
                ia.insertlayout(pgname);
                ia.deletepod(pgname);
                for (int count = 0; count < cnvMain.Children.Count; count++)
                { 
                    ia.insertpod(((_videoConference.ctlMyPOD)cnvMain.Children[count]).Text,((_videoConference.ctlMyPOD)cnvMain.Children[count]).ControlName,((_videoConference.ctlMyPOD)cnvMain.Children[count]).Height, ((_videoConference.ctlMyPOD)cnvMain.Children[count]).Width, double.Parse(((_videoConference.ctlMyPOD)cnvMain.Children[count]).GetValue(InkCanvas.LeftProperty).ToString()), double.Parse(((_videoConference.ctlMyPOD)cnvMain.Children[count]).GetValue(InkCanvas.TopProperty).ToString()), pgname,((_videoConference.ctlMyPOD)cnvMain.Children[count]).ModID.ToString());
                }
                ((Menu)((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvmenu")).FindName("mnu")).IsEnabled = true;
                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
                ((RowDefinition)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("rd1")).Height = GridLength.Auto;
                ((InkCanvas)this.Parent).Children.Remove(this);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--btnUpdate_Click()--:--");
                clsUtility.LogException = ex;
                //MessageBox.Show("1videoConference--:--GenerateModule.xaml.cs--:--btnUpdate_Click()--:--" + ex.Message);
            }
        }

        void cmbModuleName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        public void btnadd(object o, EventArgs e)
        {
            if (cmbModuleName.SelectedItem == null)
            {
                MessageBox.Show("Select the module name");
                return;
            }
            else
            {
                if (textBox1.Text == "" || textBox1.Text == null)
                {
                    MessageBox.Show("enter the title for module");
                    return;
                }
                else
                {
                    ctlMyPOD c = new ctlMyPOD();
                    c.Height = 200;
                    c.Width = 200;
                    c.SetValue(InkCanvas.LeftProperty, 120.0 + count);
                    c.SetValue(InkCanvas.TopProperty, 120.0 + count);
                    c.Text = textBox1.Text;
                    c.ControlName = cmbModuleName.SelectionBoxItem.ToString();
                    DataSet dmodid = ia.getmodid(c.ControlName);
                    Label l = new Label();
                    l.Name = "ctlname";
                    l.Content = cmbModuleName.SelectionBoxItem.ToString();
                    l.Background = ((Rectangle)c.FindName("rctTitle")).Fill;
                    l.Foreground = ((Rectangle)c.FindName("rctTitle")).Stroke;
                    l.Height = ((Label)c.FindName("lblTitle")).Height;
                    l.Width = ((Rectangle)c.FindName("rctTitle")).Width;
                    ((Canvas)c.FindName("cnvBody")).Children.Add((UIElement)l);
                    ((Canvas)c.FindName("cnvBody")).Tag = cmbModuleName.SelectionBoxItem.ToString();
                    if (dmodid.Tables.Count > 0 && dmodid.Tables[0].Rows.Count > 0)
                    {
                        
                        for(int id=0;id<dmodid.Tables[0].Rows.Count;id++)
                        {
                            c.ModID =int.Parse(dmodid.Tables[0].Rows[id][0].ToString());
                            //((Canvas)c.FindName("cnvBody")).Tag = dmodid.Tables[0].Rows[id][0].ToString();
                        }
                    }
                    cnvMain.Children.Add(c);
                    c = null;                    
                    textBox1.Clear();

                }
            }
        }

        void cnvMain_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (UserInfo.MyRole == "Host")
            {
                switch (cnvMain.Tag.GetType().ToString())
                {
                    case "_videoConference.ctlMyPOD":
                        pLeft = double.Parse(((ctlMyPOD)cnvMain.Tag).GetValue(InkCanvas.LeftProperty).ToString());
                        pTop = double.Parse(((ctlMyPOD)cnvMain.Tag).GetValue(InkCanvas.TopProperty).ToString());
                        pWidth = ((ctlMyPOD)cnvMain.Tag).Width;
                        pHeight = ((ctlMyPOD)cnvMain.Tag).Height;
                        if (UserInfo.strMeetingMode == "Meeting Mode")
                        {
                            PeerServices.channelPodResize.svcResetPod(pLeft, pTop, pWidth, pHeight, ((ctlMyPOD)cnvMain.Tag).PID,UserInfo.MeetingID);
                            //channelPodResize.svcResetPod(pLeft, pTop, pWidth, pHeight, ((ctlMyPOD)cnvMain.Tag).PID);
                            //ResetPODProxy.svcResetPod(pLeft, pTop, pWidth, pHeight, ((ctlMyPOD)cnvMain.Tag).PID);
                        }
                        break;
                }


                if (cnvMain.Children.Count > 0)
                {
                    for (int i = 0; i < cnvMain.Children.Count; i++)
                    {
                        if (((System.Windows.Controls.Button)((ctlMyPOD)cnvMain.Children[i]).FindName("btnMaximized")).IsPressed)
                        {
                            ((ctlMyPOD)cnvMain.Children[i]).maxid = !((ctlMyPOD)cnvMain.Children[i]).maxid;
                            if (UserInfo.strMeetingMode == "Meeting Mode")
                            {
                                PeerServices.channelPodResize.svcMaxPod(oldLeft, oldTop, oldWidth, oldHeight, ((ctlMyPOD)cnvMain.Children[i]).PID, ((ctlMyPOD)cnvMain.Children[i]).maxid,UserInfo.MeetingID);
                                //channelPodResize.svcMaxPod(oldLeft, oldTop, oldWidth, oldHeight, ((ctlMyPOD)cnvMain.Children[i]).PID, ((ctlMyPOD)cnvMain.Children[i]).maxid);
                                //ResetPODProxy.svcMaxPod(oldLeft, oldTop, oldWidth, oldHeight, ((ctlMyPOD)cnvMain.Children[i]).PID, ((ctlMyPOD)cnvMain.Children[i]).maxid);                               
                            }
                            break;
                        }
                        if (((System.Windows.Controls.Button)((ctlMyPOD)cnvMain.Children[i]).FindName("btnMinimized")).IsPressed)
                        {
                            if (UserInfo.strMeetingMode == "Meeting Mode")
                            {
                                PeerServices.channelPodResize.svcMinPod(((ctlMyPOD)cnvMain.Children[i]).PID,UserInfo.MeetingID);
                                //channelPodResize.svcMinPod(((ctlMyPOD)cnvMain.Children[i]).PID);
                                //ResetPODProxy.svcMinPod(((ctlMyPOD)cnvMain.Children[i]).PID);                              
                            }
                            break;
                        }
                    }
                }
            }
        }

        public void cnvMain_MouseMove(object o, MouseEventArgs e)
        {
            try
            {
                if (UserInfo.MyRole == "Host")
                {
                    switch (cnvMain.Tag.GetType().ToString())
                    {

                        case "_videoConference.ctlMyPOD":
                            if (((_videoConference.ctlMyPOD)cnvMain.Tag).mDown == true)
                            {
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).SetValue(InkCanvas.LeftProperty, e.GetPosition(cnvMain).X - ((_videoConference.ctlMyPOD)cnvMain.Tag).TitleX);
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).SetValue(InkCanvas.TopProperty, e.GetPosition(cnvMain).Y - ((_videoConference.ctlMyPOD)cnvMain.Tag).TitleY);
                            }
                            else if (((_videoConference.ctlMyPOD)cnvMain.Tag).mHeight == true && ((_videoConference.ctlMyPOD)cnvMain.Tag).mWidth == true)
                            {
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).Width = e.GetPosition(((_videoConference.ctlMyPOD)cnvMain.Tag)).X;
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).Height = e.GetPosition(((_videoConference.ctlMyPOD)cnvMain.Tag)).Y;
                            }
                            else if (((_videoConference.ctlMyPOD)cnvMain.Tag).mHeight == true && ((_videoConference.ctlMyPOD)cnvMain.Tag).mWidth == false)
                            {
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).Height = e.GetPosition(((_videoConference.ctlMyPOD)cnvMain.Tag)).Y;
                            }
                            else if (((_videoConference.ctlMyPOD)cnvMain.Tag).mHeight == false && ((_videoConference.ctlMyPOD)cnvMain.Tag).mWidth == true)
                            {
                                ((_videoConference.ctlMyPOD)cnvMain.Tag).Width = e.GetPosition(((_videoConference.ctlMyPOD)cnvMain.Tag)).X;
                            }
                            mMove = true;
                            break;
                    }
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--GenerateModule.xaml.cs--:--cnvMain_MouseMove()--:--");
                clsUtility.LogException = exp;
            }
        }

        //public void cnvMain_KeyDown(object o, KeyEventArgs e)
        //{
        //    if (UserInfo.strMeetingMode == "Design Mode")
        //    {
        //        if (e.Key == Key.Delete)
        //        {
        //            MessageBoxResult r;
        //            r = MessageBox.Show("Are you sure to delete this pod", "Pod Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
        //            if (r == MessageBoxResult.Yes)
        //            {
        //                try
        //                {
        //                    ia.delpodid(((ctlMyPOD)cnvMain.Tag).PID.ToString());
        //                }
        //                catch (Exception exp)
        //                {
        //                    exp.Data.Add("My Key", "1videoConference--:--ctlPOD.xaml.cs--:--ctlMyPOD_KeyDown()--:--");
        //                    clsUtility.LogException = exp;
        //                }
        //            }
        //        }
        //    }
        //}

        public void ShowDirectory(DirectoryInfo dir)
        {
            foreach (FileInfo file in dir.GetFiles())
            {
                int hj = al.Add(file.FullName);
            }
            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                ShowDirectory(subDir);
            }
        }        

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    
        }

        private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                { 
                   
                }        
                disposed = true;

            }
        }

        ~GenerateModule()
        {
            Dispose(false);
        }
    }
}